package features.advance.jdk.thread.pool;

import java.util.concurrent.*;

/**
 * @author LIN
 * @date 2022-03-18 14:01
 */
public class MyThreadPoolDemo {

    static BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(300);

    public static void main(String[] args) throws InterruptedException {
//        TestCase.test_new_theadpool();
        TestCase.test_thread_pool();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(3000L);
                        blockingQueue.add("aaa");

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.setName("往阻塞队列里加数据");
        thread.start();

    }


    public static class TestCase{
        public static void test_avaliable_processors(){
            int availableProcessors = Runtime.getRuntime().availableProcessors();
            System.out.println(availableProcessors);
        }
        public static void test_new_theadpool(){
            System.out.printf("%s%s%n","maximumPoolSize:",Runtime.getRuntime().availableProcessors() * 2);
            ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                    Runtime.getRuntime().availableProcessors() * 2,
                    Runtime.getRuntime().availableProcessors() * 2,
                    // 池允许最大线程数，当线程数量达到corePoolSize，且workQueue队列塞满任务之后，继续创建线程
                    1L,
                    // 超过corePoolSize之后的"临时线程"的存活时间。
                    TimeUnit.SECONDS,
                    new LinkedBlockingDeque<>(3),
                    // 当前线程数超过corePoolSize时，新的任务会处在等待状态，
                    // 并存在workQueue中，BlockingQueue是一个先进先出的阻塞
                    // 式队列实现，底层实现会涉及Java并发的AQS机制
                    Executors.defaultThreadFactory(),
                    /*
                     * 四大拒绝策略
                     * 1、CallerRunsPolicy 交给调用线程执行该任务
                     * 2、AbortPolicy 直接拒绝，抛出RejectedExecutionException异常
                     * 3、DiscardPolicy 丢弃掉刚提交的任务
                     * 4、DiscardOldestPolicy 丢弃掉工作队列最先加入的未执行的任务，将刚提交的任务放置队尾
                     */
                    new ThreadPoolExecutor.CallerRunsPolicy()
            );
            try{

                for (int i = 0; i < 60; i++) {
                    int finalI = i;
                    poolExecutor.execute(()->{
                        System.out.printf("%s:%s%n",Thread.currentThread().getName(),finalI);
                    });
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                poolExecutor.shutdown();
            }
        }


        public static void test_thread_pool(){
            ExecutorService executorService = Executors.newFixedThreadPool(8);
            for (int i = 0; i < 15; i++) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                        try {
                            String take = blockingQueue.take();
                            System.out.println(Thread.currentThread().getName()+"======>"+take);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

        }


    }



}
